var mongodb = require('mongodb');
var BSON = require('mongodb').pure().BSON;
var ObjectID = require("mongodb").ObjectID;
/*
if (process.env.VCAP_SERVICES) {
    var env = JSON.parse(process.env.VCAP_SERVICES);
	var mongo = env['mongodb-1.8'][0]['credentials'];
} else {
	var mongo = {
		"hostname" : "ds039078.mongolab.com",
		"port" : 39078,
		"username" : "admin",
		"password" : "mta2013",
		"db" : "activiteam"
	};
}

var generate_mongo_url = function(obj) {
	obj.hostname = (obj.hostname || 'localhost');
	obj.port = (obj.port || 27017);
	obj.db = (obj.db || 'test');

	if (obj.username && obj.password) {
		return "mongodb://" + obj.username + ":" + obj.password + "@"
				+ obj.hostname + ":" + obj.port + "/" + obj.db;
	} else {
		return "mongodb://" + obj.hostname + ":" + obj.port + "/" + obj.db;
	}
};
*/
var mongourl = process.env.MONGOLAB_DB ? process.env.MONGOLAB_DB : 'mongodb://admin:mta2013@ds039078.mongolab.com:39078/activiteam';//generate_mongo_url(mongo);

exports.createComment = function(req, res) {

	console.log("createTeamComment start");

	var doc = req.body;

	var newComment = {
		commentText : doc.commentText,
		userNickName : doc.userNickName,
		itemid : doc.itemid,
		itemType : doc.itemType
	};

	var db;
	mongodb.connect(mongourl, function(err, conn) {
		if (err) {
			res.send({
				"EROOR Connection to DB" : err.message
			});
			console.log("EROOR Connection to DB:" + err.message);
		} else {
			console.log("SUCCESS Connection to DB");
			db = conn;
			db.collection("comments", function(err, collection) {
				if (err) {
					console.log("EROOR Connection to comments DB:"
							+ err.message);
					res.send({
						"EROOR Connection to comments DB" : err.message
					});
					db.close();
					console.log("close DB");
				} else {
					console.log("SUCCESS Connection to comments documents");

					collection.insert(newComment, function(err, result) {
						if (err) {
							res.send({
								"EROOR" : "create comment" + err.message
							});
							console.log("EROOR create comment" + err.message);
						} else {
							res.send({
								"SUCCESS" : "created new comment"
							});
							console.log("SUCCESS created new comment id: "
									+ result[0]._id);
						}
						db.close();
						console.log("close DB");
					});

				}
			});
		}
	});
};

exports.deleteComment = function(req, res) {

	console.log("deleteComment start");

	var db;
	mongodb.connect(mongourl, function(err, conn) {
		if (err) {
			res.send({
				"EROOR Connection to DB" : err.message
			});
			console.log("EROOR Connection to DB:" + err.message);
		} else {
			console.log("SUCCESS Connection to DB");
			db = conn;
			db.collection("comments", function(err, collection) {
				if (err) {
					console.log("EROOR Connection to comments DB:"
							+ err.message);
					res.send({
						"EROOR Connection to comments DB" : err.message
					});
					db.close();
					console.log("close DB");
				} else {
					console.log("SUCCESS Connection to comments documents");

					collection.remove({
						"_id" : ObjectID(req.params.commentid)
					}, 1, function(err, comment) {
						if (err) {
							res.send({
								"EROOR" : "delete comment" + err.message
							});
							console.log("EROOR delete comment" + err.message);
						} else if (!comment) {
							console.log("EROOR delete comment not found");
						} else {
							res.send({
								"SUCCESS" : "delete comment"
							});
							console.log("SUCCESS delete comment");
						}
						db.close();
						console.log("close DB");
					});
				}
			});
		}
	});
};

exports.readComments = function(req, res) {

	console.log("readComments start");

	var db;
	mongodb.connect(mongourl, function(err, conn) {
		if (err) {
			res.send({
				"EROOR Connection to DB" : err.message
			});
			console.log("EROOR Connection to DB:" + err.message);
		} else {
			console.log("SUCCESS Connection to DB");
			db = conn;
			db.collection("comments", function(err, collection) {
				if (err) {
					console.log("EROOR Connection to comments DB:"+ err.message);
					res.send({
						"EROOR Connection to comments DB" : err.message
					});
					db.close();
					console.log("close DB");
				} else {
					console.log("SUCCESS Connection to comments documents");

					collection.find({
						"itemid" : req.params.itemid
					}).toArray(function(err, comments) {
						db.close();
						console.log("close DB");
						if (err) {
							res.send({
								"EROOR" : "get comments" + err.message
							});
							console.log("EROOR get comments" + err.message);
						} else {
							res.send({
								"SUCCESS" : comments
							});
							console.log("SUCCESS get comments");
						}
					});
				}
			});
		}
	});
};